package log350.restaurant;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import com.example.widget.NumberPicker;

import Class.Commande;
import Class.ItemCommande;
import Class.ItemMenu;
import Class.SingletonCommandeCourante;
import Class.SingletonCommandesEnCours;
import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class Commandes extends Activity {
	ListView viewMenuDetail;
	NumberPicker num;
	DecimalFormat df = new DecimalFormat("#.##");
	int selectedItem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_commandes);
		
		viewMenuDetail = (ListView) findViewById(R.id.ListItem);
		viewMenuDetail.setVisibility(0);
		viewMenuDetail.setEnabled(true);
		LoadList();

		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_commandes, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.Factures:
			Intent intent = new Intent(this, Factures.class);
			startActivity(intent);
			return (true);
		case R.id.Livraison:
			Intent intentLivraison = new Intent(this, Livraisons.class);
			startActivity(intentLivraison);
			return (true);
		}
		return super.onOptionsItemSelected(item);
	}

	private void LoadList() {

		ArrayList<HashMap<String, String>> ListHashMapItem = new ArrayList<HashMap<String, String>>();

		ArrayList<ItemCommande> ListItem = new ArrayList<ItemCommande>();
		ListItem = SingletonCommandeCourante.getCommandeCourante().getCommande().getItems();

		for (ItemCommande itemCommande : ListItem) {
			HashMap<String, String> mapItemMenu;

			mapItemMenu = new HashMap<String, String>();

			mapItemMenu.put("nom", itemCommande.getItem().getNom());
			mapItemMenu.put("description", itemCommande.getItem().getDescription());

			String prixEtQte = itemCommande.getItem().getPrix().toString() + " (" + df.format(itemCommande.getQuantite()) + ")";
			mapItemMenu.put("prix", prixEtQte);

			ListHashMapItem.add(mapItemMenu);
		}

		SimpleAdapter mSchedule = new SimpleAdapter(getApplicationContext(), ListHashMapItem, R.layout.affichage_details_menu, new String[] { "nom",
				"description", "prix" }, new int[] { R.id.nom, R.id.description, R.id.prix });
		viewMenuDetail.setAdapter(mSchedule);
		viewMenuDetail.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> a, View v, int position, long id) {
				HashMap<String, String> map = (HashMap<String, String>) viewMenuDetail.getItemAtPosition(position);
				selectedItem = position;
				LayoutInflater inflater = getLayoutInflater();
				final View dialoglayout = inflater.inflate(R.layout.layout_num_picker, (ViewGroup) Commandes.this.getCurrentFocus(), false);
				AlertDialog.Builder builder = new AlertDialog.Builder(Commandes.this);
				builder.setView(dialoglayout);
				builder.setTitle("Entrer le nombre de " + map.get("nom"));
				num = (NumberPicker) dialoglayout.findViewById(R.id.NumPicker);
				
				String qte = map.get("prix");
				qte = qte.substring(qte.indexOf("(") + 1);
				qte = qte.substring(0, qte.length() -1);
				num.setCurrent(Integer.valueOf(qte));
				builder.setPositiveButton("Modifier", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int id) {
						if (num.getCurrent() <= 0)
							SingletonCommandeCourante.getCommandeCourante().getCommande().getItems().remove(selectedItem);
						else
							SingletonCommandeCourante.getCommandeCourante().getCommande().getItems().get(selectedItem).SetQuantite(num.getCurrent());

						LoadList();
					}

				});
				builder.setNegativeButton("Supprimer", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int id) {
							SingletonCommandeCourante.getCommandeCourante().getCommande().getItems().remove(selectedItem);
						LoadList();
					}

				});
				// custom dialog
				AlertDialog alert = builder.create();
				alert.show();

			}
		});

	}

}
